using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Lexalytics;

namespace SalienceEngineDemo
{
    public partial class ctrlGetDocumentCategoryTopics : UserControl, DemoControl
    {
        public RichTextBox oTextBox;

        public ctrlGetDocumentCategoryTopics(RichTextBox oTextBox)
        {
            InitializeComponent();
            this.oTextBox = oTextBox;

            // Set Process button state
            EnableProcessButton();
        }

        public TreeNode GenerateNode(SalienceTopic oTopic)
        {
            TreeNode node = new TreeNode(oTopic.sTopic + " (" + oTopic.fScore + ")");
            foreach (SalienceTopic oChild in oTopic.vChildren)
            {
                node.Nodes.Add(GenerateNode(oChild));
            }
            return node;
        }

        public void processText()
        {
            treeResults.Nodes.Clear();

            try
            {
                SalienceDemo.oSalienceSession.PrepareText(this.oTextBox.Text);
                List<SalienceTopic> oResults = SalienceDemo.oSalienceSession.GetDocumentCategories();

                if (oResults.Count > 0)
                {
                    foreach (SalienceTopic oTopic in oResults)
                    {
                        treeResults.Nodes.Add(GenerateNode(oTopic));
                    }
                }
                else
                {
                    MessageBox.Show("No document categories were retrieved for this content.", "GetDocumentCategories()");
                }
            }
            catch (SalienceException e)
            {
                MessageBox.Show(e.Message, "Salience Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            processText();
        }

        private void getDocumentTags_VisibleChanged(object sender, EventArgs e)
        {
            treeResults.Nodes.Clear();
        }

        #region DemoControl interface methods
        public void btnOptions_Click(object sender, EventArgs e)
        {
            ((SalienceDemo)this.Parent).StartOptionsDialog((int)Dialogs.Options.OptionSections.DOCUMENTS);
            EnableProcessButton();
        }

        public void SetFontSize()
        {
            this.treeResults.Font = new Font(this.oTextBox.Font.FontFamily, Properties.Settings.Default.fFontSize);
        }

        public void EnableProcessButton()
        {
            // Process button is enabled if text box has text
            this.btnProcess.Enabled = (oTextBox.Text.Trim().Length > 0);
        }
        #endregion
    }
}
